// Background tone 背景提示音效

class Tone{
	constructor({url,time=500,volume=5,sessionCategory='playback'}) {
	    this.url=url //背景提示音链接
		this.sessionCategory=sessionCategory//是否后台播放 但会终止其他声音
		this.volume=volume //音量
		this.time=time //防抖时间
		this.timer=null //定时器对象 
		this.audioContext=uni.createInnerAudioContext() //创建背景音效播放器对象
		
		this.audioContext.onPlay(()=>{
			// console.log('正在播放')
		})
		this.audioContext.onEnded(()=>{
			// console.log('正在播放')
			clearTimeout(this.timer)
			this.timer=null
			
		})
	}
	play=(url)=>{
		
			if(!this.timer){
				this.audioContext.src=url || this.url
				 
				// console.log(this.audioContext)
				this.timer=setTimeout(()=>{
					this.audioContext.play()
				},this.time)
			}
							
	}
		
	destroy=()=>{
		this.audioContext.destroy()
	}
}


export default Tone